﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Collections.Specialized;
namespace Shop.Implementation.Customer
{
    public class ICustomerFavor_V1 : Interface.Customer.ICustomerFavor
    {
        #region 删除收藏
        public override Dictionary<string, object> Delete(int idcus)
        {
            NameValueCollection nv = System.Web.HttpContext.Current.Request.QueryString;
            string cinvcode = nv["cinvcode"].GetVal_String();
            string cmdsql = @"delete from CustomerFavourite where IDCus=@IDCus and cInvCode=@cInvCode ";
            using (SqlConnection cn = new SqlConnection(Common.HandleSQL.cConnstr))
            {
                try
                {
                    cn.Open();
                    SqlCommand cncmd = new SqlCommand();
                    cncmd.Connection = cn;
                    cncmd.CommandText = cmdsql;
                    cncmd.Parameters.Add(new SqlParameter("@IDCus", idcus));
                    cncmd.Parameters.Add(new SqlParameter("@cInvCode", cinvcode));
                    cncmd.ExecuteNonQuery();
                    cncmd.Parameters.Clear();
                }
                finally
                { cn.Close(); cn.Dispose(); }
            }
            return Common.ReturnValue.Get("成功！", true);
        }
        #endregion

        #region 收藏商品
        public override Dictionary<string, object> Save(int idcus )
        {
            NameValueCollection nv = System.Web.HttpContext.Current.Request.QueryString;
            string cinvcode = nv["cinvcode"].GetVal_String();
            string cmdsql = @"if (not exists(select top 1 1 from CustomerFavourite where idcus = @IDCus and cInvCode = @cInvCode))
insert into CustomerFavourite(IDCus, cInvCode, dDate, cFromIP)
values(@IDCus, @cInvCode, GETDATE(), @cFromIP)";
            using (SqlConnection cn = new SqlConnection(Common.HandleSQL.cConnstr))
            {
                try
                {
                    cn.Open();
                    SqlCommand cncmd = new SqlCommand();
                    cncmd.Connection = cn;
                    cncmd.CommandText = cmdsql;
                    cncmd.Parameters.Add(new SqlParameter("@IDCus", idcus));
                    cncmd.Parameters.Add(new SqlParameter("@cInvCode", cinvcode));
                    cncmd.Parameters.Add(new SqlParameter("@cFromIP", Common.HandleLog.GetIPAddress()));
                    cncmd.ExecuteNonQuery();
                    cncmd.Parameters.Clear();
                    cmdsql = "delete from CustomerFavourite where cinvcode not in (select cinvcode+'-'+cInvCodeSub from inventorysale where istatus=0)";
                    cncmd.CommandText = cmdsql;
                    cncmd.ExecuteNonQuery();
                }
                finally
                { cn.Close(); cn.Dispose(); }
            }
            return Common.ReturnValue.Get("成功！", true);
        }
        #endregion
    }
}